{% extends '../_manage.html' %}

{% block title %} {{ _('All Navigations') }} {% endblock %}

{% block head %}
<script>
$(function() {
    getJSON('/api/navigations', function (err, data) {
        if (err) {
            return fatal(err);
        }
        initVM(data.navigations);
    });
});

function initVM(navigations) {
    var vm = new Vue({
        el: '#vm',
        data: {
            navigations: navigations
        },
        methods: {
            sort: function(event) {
                var ids = [];
                for (var i = 0; i < this.navigations.length; i ++) {
                    ids.push(this.navigations[i].id);
                }
                postJSON('/api/navigations/all/sort', {
                    ids: ids
                }, function(err, result) {
                    if (err) {
                        return error(err);
                    }
                    refresh();
                });
            },
            find: function(id) {
                for (var i = 0; i < this.navigations.length; i ++) {
                    var c = this.navigations[i];
                    if (c.id===id) {
                        return i;
                    }
                }
                throw 'logic error';
            },
            swap: function(index1, index2) {
                var obj = this.navigations[index2];
                this.navigations.splice(index2, 1);
                this.navigations.splice(index1, 0, obj);
                $('#sort').show();
            },
            moveUp: function(id) {
                var index = this.find(id);
                if (index===(-1) || index===0) {
                    return;
                }
                this.swap(index - 1, index);
            },
            moveDown: function(id) {
                var index = this.find(id);
                if (index===(-1) || index===(this.navigations.length-1)) {
                    return;
                }
                this.swap(index, index + 1);
            },
            deleteNavigation: function(id) {
                var n = this.navigations[this.find(id)];
                if (confirm('Delete navigation \"' + n.name + '\". Continue?')) {
                    postJSON('/api/navigations/' + n.id + '/delete', function(err, result) {
                        if (err) {
                            return error(err);
                        }
                        refresh();
                    });
                }
            }
        }
    });
    $('#loading').hide();
    $('#vm').show();
}
</script>
{% endblock %}

{% block main %}

    <div id="error" class="uk-width-1-1">
    </div>

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> Loading...
    </div>

    <div id="vm" class="uk-width-1-1">
        <div class="uk-margin">
            <a href="javascript:refresh()" class="uk-button"><i class="uk-icon-refresh"></i> {{ _('Refresh') }}</a>
            <a href="create_navigation" class="uk-button uk-button-primary uk-float-right"><i class="uk-icon-plus"></i> {{ _('New Navigation') }}</a>
        </div>

        <table class="uk-table uk-table-hover">
            <thead>
                <tr>
                    <th width="15%">{{ _('Name') }}</th>
                    <th width="55%">URL</th>
                    <th width="15%">{{ _('Created At') }}</th>
                    <th width="15%">&nbsp;</th>
                </tr>
            </thead>
            <tbody>
                <tr v-repeat="n: navigations">
                    <td><a v-text="n.name" v-attr="href: n.url" target="_blank"></a></td>
                    <td><a v-text="n.url" v-attr="href: n.url" target="_blank"></a></td>
                    <td><span v-text="n.created_at.toDateTime()"></span></td>
                    <td>
                        <a v-on="click: moveUp(n.id)" title="Move this navigation up" href="#0" class="x-btn"><i class="uk-icon-arrow-up"></i></a>
                        <a v-on="click: moveDown(n.id)" title="Move this navigation down" href="#0" class="x-btn"><i class="uk-icon-arrow-down"></i></a>
                        <a v-on="click: deleteNavigation(n.id)" title="Delete this navigation" href="#0" class="x-btn"><i class="uk-icon-trash"></i></a>
                    </td>
                </tr>
                <tr v-if="navigations.length===0">
                    <td colspan="5">No navigation found.</td>
                </tr>
            </tbody>
        </table>

        <div id="sort" class="uk-margin" style="text-align:right; display:none">
            <button v-on="click: sort" type="button" class="uk-button uk-button-success"><i class="uk-icon-list"></i> {{ _('Save Orders') }}</button>
        </div>
    </div>

{% endblock %}
